package com.yunyao.dao.service.bamu.impl;

import com.yunyao.common.core.BaseContext;
import com.yunyao.common.core.UserInfoRequest;
import com.yunyao.common.dto.ReceiptSaveDto;
import com.yunyao.common.dto.SubmitReceiptDto;
import com.yunyao.common.web.util.StringUtils;
import com.yunyao.dao.model.BamuReceipt;
import com.yunyao.dao.mapper.BamuReceiptMapper;
import com.yunyao.dao.service.bamu.IBamuReceiptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * <p>
 * 收据信息表 服务实现类
 * </p>
 *
 * @author xingjishuai
 * @since 2024-07-02
 */
@Service
public class BamuReceiptServiceImpl extends ServiceImpl<BamuReceiptMapper, BamuReceipt> implements IBamuReceiptService {


    @Override
    public boolean submitReceipt(@Valid @NotNull SubmitReceiptDto dto) {
        return lambdaUpdate()
                .eq(BamuReceipt::getBizId, dto)
                .eq(BamuReceipt::getBizType, dto.getBizType())
                .set(BamuReceipt::getApplyUserId, dto.getApplyUserId())
                .set(BamuReceipt::getStatus, dto.getStatus())
                .set(StringUtils.isNotBlank(dto.getReceiptMark()), BamuReceipt::getReceiptMark, dto.getReceiptMark())
                .set(StringUtils.isNotBlank(dto.getApplyMark()), BamuReceipt::getApplyMark, dto.getApplyMark())
                .update();
    }

    @Override
    public boolean saveOrUpd(@Valid @NotNull ReceiptSaveDto dto) {
        UserInfoRequest userInfo = BaseContext.getUserInfo();
        BamuReceipt receipt = new BamuReceipt();
        Date date = new Date();
        receipt.setCreatedAt(date);
        receipt.setUpdatedAt(date);
        receipt.setCreatedBy(userInfo.getId());
        receipt.setCreateDept(userInfo.getDeptId());
        receipt.setUpdatedBy(userInfo.getId());
        receipt.setApplyDate(dto.getApplyDate());
        receipt.setApplyMark(dto.getApplyMark());
        receipt.setBizId(dto.getBizId());
        receipt.setBizType(dto.getBizType());
        receipt.setStatus(dto.getStatus());
        receipt.setReceiptUnitName(dto.getReceiptUnitName());
        return save(receipt);
    }

    @Override
    public BamuReceipt detail(@NotBlank String bizId, @NotBlank String bizType) {
        return lambdaQuery()
                .eq(BamuReceipt::getBizId, bizId)
                .eq(BamuReceipt::getBizType, bizType)
                .one();

    }

}
